using System.Collections;
using ProtoModels;
using UnityEngine;

public class LeaderboardPositionDisplay : MonoBehaviour
{
	public class OldPositionInfo
	{
		public bool hasOldPosition;

		public PlayerPositions.PlayerPosition oldPosition;
	}

	public UILabel myLabel;

	public UILabel myLoader;

	public UILabel leadPosChangeLabel;

	public GameObject leadPosChangeObject;

	private string _pid = string.Empty;

	private Coroutine loadingDotsUpdate;

	public PlayerPositions.PlayerPosition myPosition { get; protected set; }

	private void OnEnable()
	{
		if (myPosition == null)
		{
			UITools.ChangeText(myLabel, string.Empty);
			StartLoadingDisplay();
			SetActiveLeadPosChangeObject(false);
		}
	}

	private void OnDisable()
	{
		StopLoadingDisplay();
		myPosition = null;
		_pid = string.Empty;
		SetActiveLeadPosChangeObject(false);
	}

	private void SetActiveLeadPosChangeObject(bool isActive)
	{
		if (leadPosChangeObject != null)
		{
			leadPosChangeObject.gameObject.SetActive(isActive);
		}
	}

	public void FetchPlayerInfo(string pid, OldPositionInfo oldPosInfo = null)
	{
		Debug.Log("FETCHPLAYERINFO");
		_pid = pid;
		myPosition = null;
		SetActiveLeadPosChangeObject(false);
		UITools.ChangeText(myLabel, string.Empty);
		StartLoadingDisplay();
		if (pid == null)
		{
			return;
		}
		BehaviourSingletonInit<LeaderboardPositionInfoManager>.instance.GetPlayerPostion(pid, delegate(PlayerPositions.PlayerPosition playerPosition)
		{
			Debug.Log("FETCHPLAYERINFO - FINISHED");
			StopLoadingDisplay();
			string empty = string.Empty;
			if (playerPosition == null || playerPosition.leadPositions.Count <= 0)
			{
				empty = "Not Ranked ";
			}
			else
			{
				Lead.Leaderboard.Prize prize = playerPosition.leadPositions[0].prize;
				empty = ((prize != null && !(prize.name == string.Empty)) ? prize.name : (playerPosition.leadPositions[0].leaderboardIndex + 1 + " place"));
				if (oldPosInfo != null && oldPosInfo.hasOldPosition)
				{
					int num = ((oldPosInfo.oldPosition == null || oldPosInfo.oldPosition.leadPositions.Count <= 0) ? playerPosition.leadPositions[0].totalInLead : (oldPosInfo.oldPosition.leadPositions[0].leaderboardIndex + 1));
					int num2 = playerPosition.leadPositions[0].leaderboardIndex + 1;
					int num3 = num - num2;
					if (num3 > 0)
					{
						SetActiveLeadPosChangeObject(true);
						UITools.ChangeText(leadPosChangeLabel, num3.ToString());
					}
				}
			}
			UITools.ChangeText(myLabel, empty);
			myPosition = playerPosition;
		});
	}

	private void StartLoadingDisplay()
	{
		if (base.gameObject.activeInHierarchy && loadingDotsUpdate == null)
		{
			myLoader.gameObject.SetActive(true);
			loadingDotsUpdate = StartCoroutine(DoShowLoadingDisplay());
		}
	}

	private IEnumerator DoShowLoadingDisplay()
	{
		int dotsCount = 0;
		while (true)
		{
			if (dotsCount > 2)
			{
				dotsCount = 0;
			}
			string dotsString = string.Empty;
			for (int i = 0; i <= dotsCount; i++)
			{
				dotsString += ".";
			}
			UITools.ChangeText(myLoader, dotsString);
			yield return new WaitForSeconds(0.5f);
			dotsCount++;
		}
	}

	private void StopLoadingDisplay()
	{
		if (loadingDotsUpdate != null)
		{
			StopCoroutine(loadingDotsUpdate);
		}
		loadingDotsUpdate = null;
		myLoader.gameObject.SetActive(false);
	}
}
